package com;

import java.util.ArrayList;
import java.util.List;

import com.iface.IAlarmGenerator;
import com.iface.ILogProcessor;
import com.iface.ILogReader;
import com.iface.IMessageObserver;
import com.iface.IUserNotifier;
import com.notifier.EmailObserver;
import com.notifier.SMSObserver;
import com.notifier.UserNotifier;
import com.util.LogUtil;

/**
 * Sahabjee will create and injection, all objects that Bahaddur
 * may need.
 *   
 * @author shree
 *
 */
public class SahabJee {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Bahaddur bahaddur = new Bahaddur();
		ILogReader reader = getLogReader();
		ILogProcessor logProcessor = getLogProcessor();
		IAlarmGenerator alarmGenerator = getAlarmGenerator();
		alarmGenerator.setMessageObservers(getMessageObservers());
		IUserNotifier userNotifier = getUserNotifier();
		
		bahaddur.setReader(reader);
		bahaddur.setLogProcessor(logProcessor);
		bahaddur.setAlarmGenerator(alarmGenerator);
		bahaddur.setUserNotifier(userNotifier);
		bahaddur.monitorLog();
	}

	/**
	 * @return
	 */
	private static List<IMessageObserver> getMessageObservers() {
		List<IMessageObserver> messageObservers = new ArrayList<IMessageObserver>();
		messageObservers.add(new EmailObserver());
		messageObservers.add(new SMSObserver());
		return messageObservers;
	}

	/**
	 * @return
	 */
	private static UserNotifier getUserNotifier() {
		return new UserNotifier();
	}

	/**
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static IAlarmGenerator getAlarmGenerator() {
		IAlarmGenerator object = null;
		try {
			Class<IAlarmGenerator> clazz = (Class<IAlarmGenerator>) Class
					.forName(LogUtil.getValueFromProperties(
							"classNamesConfig.properties", "AlarmGenerator"));
			object = clazz.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static ILogProcessor getLogProcessor() {
		ILogProcessor object = null;
		try {
			Class<ILogProcessor> clazz = (Class<ILogProcessor>) Class
					.forName(LogUtil.getValueFromProperties(
							"classNamesConfig.properties", "LogProcessor"));
			object = clazz.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * Reads config and gets LogReader.
	 * 
	 * @return LogReader
	 */
	private static ILogReader getLogReader() {
		ILogReader object = null;
		try {
			Class<ILogReader> clazz = (Class<ILogReader>) Class.forName(LogUtil
					.getValueFromProperties("classNamesConfig.properties",
							"LogReader"));
			object = clazz.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

}
